<?php
namespace app\store\controller;
use think\Db;
use think\request;
use think\Controller;
class Report extends Controller
{
	protected function _initialize() {
		header('Access-Control-Allow-Origin:*');
		$type=get_brower_type();
	}
	public function index(){
		$agent_id=session('agent_id');
		$type=input("type")?input("type"):1;
		$where['agent_id']=$agent_id;
		if($type==2){//按周
			$wheret="week";
			$time=date("Y-m-d",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));
			$time.="到".date('Y-m-d',time());
		}else if($type==3){//按月
			$time=date("Y-m-d",mktime(0, 0 , 0,date("m"),1,date("Y")));
			$time.="到".date('Y-m-d',time());
			$wheret="month";
		}else{//按日
			$time=date('Y-m-d',time());
			$wheret="today";
		}
		$where['order_type']=["in","1,2,6,7"];
		$field="count(id) as ids,sum(balance) as balance,avg(balance) as avg,"
				." SUM(CASE WHEN agent_score<0 THEN agent_score ELSE 0 END) as zscore,"
		 		." SUM(CASE WHEN agent_score>0 THEN agent_score ELSE 0 END) as fscore";
		$res=db("log_paylog_copy")->field($field)->where($where)->whereTime("add_time",$wheret)->find();
		if($res&&$res['avg']){
			$res['avg']= round($res['avg'],2);
		}else{
			$res['ids']=0;
			$res['balance']=0;
			$res['avg']=0;
			$res['zscore']=0;
			$res['fscore']=0;
		}
		$this->assign("res",$res);
		$this->assign("type",$type);
		$this->assign("time",$time);
		return $this->fetch();
	}
}
